package com.lunwen.lunwen.api;

import com.deepoove.poi.XWPFTemplate;
import com.lunwen.lunwen.bean.Result;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("api")
public class TestController {

    @RequestMapping("/test1")
    public Result test1(){
        Result result = new Result();
        result.setCode("200");
        result.setMessage("成功");
        return result;
    }

    @RequestMapping("/test2")
    public Result test2(HttpServletResponse response){

        Result result = new Result();

        try {
            Map<String, Object> datas = new HashMap<String, Object>() {
                {
                    put("lunwewntimu", "实验室管理系统");
                    put("xinmin", "张三");
                    put("xuehao", "2017108001");
                    put("xueyuan", "计算机学院");
                    put("zhuanye", "计算机科学与技术");
                    put("banji", "计科一班");
                    put("daoshi", "王敏");
                }
            };
            ClassPathResource oldDoc = new ClassPathResource("doc/MoBan.docx");
            String filePath = oldDoc.getFile().getPath();
            String outPath = "D:/doc/outTL.docx";
            XWPFTemplate template = XWPFTemplate.compile(filePath)
                    .render(datas);
            FileOutputStream out = new FileOutputStream(outPath);
            template.write(out);
            out.flush();
            out.close();
            template.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String file_url = "D:/doc/outTL.docx";
        FileInputStream fis = null;
        ServletOutputStream outputStream = null;
        //设置下载文件名
        response.setHeader("content-disposition", "attachment;filename=outTL.docx");
        //提示下载图片
        //response.setHeader("content-type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        try {
            fis = new FileInputStream(file_url);
            outputStream = response.getOutputStream();
            int len = 1;
            byte[] b = new byte[1024];
            while((len=fis.read(b))!=-1){
                outputStream.write(b, 0, len);
            }
            fis.close();
            outputStream.close();
        } catch (FileNotFoundException e) {
            result.setCode("201");
            return result;
        } catch (IOException e) {
            result.setCode("201");
            return result;
        }

        try {
            fis.close();
            outputStream.close();
        } catch (Exception e){
            result.setCode("201");
            return result;
        }

        result.setCode("200");
        result.setMessage("成功");
        return result;
    }


}
